package vuelta.general.rss;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import vuelta.general.BaseActivity;
import vuelta.general.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageButton;

public class RssActivityOld extends BaseActivity {

	private ExpandableListAdapter adapter;
	private ImageButton mUpdateButton;
	private List<JSONObject> mJobsArray = new ArrayList<JSONObject>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rss_old);
		initToolbar();

		// Retrive the ExpandableListView from the layout
		ExpandableListView listView = (ExpandableListView) findViewById(R.id.listViewExp);

		// listView.setOnChildClickListener(new OnChildClickListener() {
		//
		// public boolean onChildClick(ExpandableListView parent, View v, int
		// groupPosition, int childPosition, long id) {
		//
		// try {
		// Intent intent = new Intent(RssActivity.this, WebViewCorridore.class);
		// intent.putExtra("url",
		// "http://www.gazzetta.it"+temp.get(groupPosition).getString("url"));
		// startActivity(intent);
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
		//
		// return true;
		// }
		//
		// });

		listView.setOnGroupClickListener(new OnGroupClickListener() {

			public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) {
				return false;
			}

		});

		adapter = new ExpandableListAdapter(this, true);
		listView.setAdapter(adapter);

		getRssFeedArticle();
		mUpdateButton = (ImageButton) findViewById(R.id.update_button);
		mUpdateButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				getRssFeedArticle();

			}
		});

	}

	private void getRssFeedArticle() {
		showProgressAsync();
		Thread t = new Thread(new Runnable() {

			public void run() {
				try {
					mJobsArray = RssReaderOld.getLatestRssFeed();
				} catch (Exception e) {
//					Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString());
				}
				mHandler.post(new Runnable() {

					public void run() {
						adapter.setList(mJobsArray);
						hideProgressAsync();
					}
				});
			}
		});
		t.start();

	}

}